﻿/*************************************************************************************

- CLR版本：        4.0.30319.42000
- 类 名 称：       QuartzUtil
- 机器名称：       YTE
- 命名空间：       AOI
- 文 件 名：       QuartzUtil
- 创建时间：       2020/12/9 9:16:22
- 作    者：       YTE
- 说   明：
- 修改时间：
- 修 改 人：
  *************************************************************************************/

using Quartz;
using Quartz.Impl;
using Quartz.Impl.Triggers;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace LD.Quartz
{
    public static class QuartzUtil
    {
        private static ISchedulerFactory sf = null;
        private static IScheduler sched = null;
        static QuartzUtil()
        {
            sf = new StdSchedulerFactory();
            getSched();
        }
        private async static void getSched()
        {
            sched = await sf.GetScheduler();
            await sched.Start();
        }

        /// <summary>
        /// 添加Job 并且以定点的形式运行
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="JobName"></param>
        /// <param name="CronTime"></param>
        /// <param name="jobDataMap"></param>
        /// <returns></returns>
        public async static void AddJob<T>(string JobName, string CronTime, Dictionary<string, object> map = null) where T : IJob
        {
            IJobDetail jobCheck = JobBuilder.Create<T>().WithIdentity(JobName, JobName + "_Group").Build();
            if (map != null)
                jobCheck.JobDataMap.PutAll(map);
            ICronTrigger CronTrigger = new CronTriggerImpl(JobName + "_CronTrigger", JobName + "_TriggerGroup", CronTime);
            await sched.ScheduleJob(jobCheck, CronTrigger);
        }

        /// <summary>
        /// 添加Job 并且以定点的形式运行
        /// </summary>
        /// <param name="IJob"></param>
        /// <param name="JobName"></param>
        /// <param name="CronTime"></param>
        /// <param name="jobData"></param>
        public async static void AddJob(Type IJob, string JobName, string CronTime, Dictionary<string, object> map = null)
        {
            IJobDetail jobCheck = JobBuilder.Create(IJob).WithIdentity(JobName, JobName + "_Group").Build();
            if (map != null)
                jobCheck.JobDataMap.PutAll(map);
            ICronTrigger CronTrigger = new CronTriggerImpl(JobName + "_CronTrigger", JobName + "_TriggerGroup", CronTime);
            await sched.ScheduleJob(jobCheck, CronTrigger);
        }

        /// <summary>
        /// 判断任务是否建立
        /// </summary>
        /// <param name="JobName"></param>
        /// <returns></returns>
        public static async Task<bool> CheckExist(string JobName)
        {
            TriggerKey triggerKey = new TriggerKey(JobName + "_CronTrigger", JobName + "_TriggerGroup");
            bool isExists = await sched.CheckExists(triggerKey);
            return isExists;
        }

        /// <summary>
        /// 暂停Job
        /// </summary>
        public static void PauseJob(string JobName = "")
        {
            if (string.IsNullOrEmpty(JobName))
                sched.PauseAll();
            else
                sched.PauseJob(new JobKey(JobName, JobName + "_Group"));
        }

        /// <summary>
        /// 恢复所有Job
        /// </summary>
        public static void ResumeJob(string JobName = "")
        {
            if (string.IsNullOrEmpty(JobName))
                sched.ResumeAll();
            else
                sched.ResumeJob(new JobKey(JobName, JobName + "_Group"));
        }

        /// <summary>
        /// 删除Job
        /// </summary>
        /// <param name="JobName"></param>
        public static void DeleteJob(string JobName)
        {
            sched.DeleteJob(new JobKey(JobName, JobName + "_Group"));
        }

        /// <summary>
        /// 修改触发器时间,需要job名,以及修改结果
        /// </summary>
        /// <param name="jobName"></param>
        /// <param name="CronTime"></param>
        public static async void UpdateTime(string JobName, string CronTime)
        {
            TriggerKey TKey = new TriggerKey(JobName + "_CronTrigger", JobName + "_TriggerGroup");
            CronTriggerImpl cti = await sched.GetTrigger(TKey) as CronTriggerImpl;
            cti.CronExpression = new CronExpression(CronTime);
            await sched.RescheduleJob(TKey, cti);
        }
    }
}
